using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading;
using WCSFContrib.PageFlow;
using PageFlowWithShoppingCartQuickstart.Store.BusinessLogic.BussinesEntites;
using PageFlowWithShoppingCartQuickstart.Store.PageFlows;

namespace PageFlowWithShoppingCartQuickstart.Store.BusinessLogic
{
    public class StoreController
    {
        private IPageFlowProvider _pageFlowProvider;
        private IPageFlow _storePageFlow;

        public StoreController(IPageFlowProvider provider)
        {
            _pageFlowProvider = provider;
        }

        public IPageFlow StorePageFlow
        {
            get
            {
                if (_storePageFlow == null)
                {
                    _storePageFlow = _pageFlowProvider.GetPageFlow(typeof(StorePageFlow));
                }
                return _storePageFlow;
            }
        }

        public ShoppingCart Cart
        {
            get
            {
                if (!StorePageFlow.UserData.Contains("Cart"))
                {
                    StorePageFlow.UserData["Cart"] = new ShoppingCart(Thread.CurrentPrincipal.Identity);
                }
                return (ShoppingCart)StorePageFlow.UserData["Cart"];
            }
            set { StorePageFlow.UserData["Cart"] = value; }
        }

        public void BrowseFail()
        {
            StorePageFlow.Navigate("addFailed");
        }

        public void ClearErrors()
        {
            StorePageFlow.Navigate("goBack");
        }

        public void AddToCart(Product product, int quantity)
        {
            ShoppingCart cart = Cart;

            CartItem cartItem = cart.Find(new Predicate<CartItem>(delegate(CartItem item)
                                                                    {
                                                                        return item.Product.ProductId == product.ProductId;
                                                                    }));
            if (cartItem == null)
            {
                cart.Add(new CartItem(product, quantity));
            }
            else
            {
                cartItem.Quantity += quantity;
            }

            Cart = cart;
            StorePageFlow.Navigate("addItem");
        }

        public void CheckoutOrder()
        {
            StorePageFlow.Navigate("doCheckout");
        }

        public void SuspendShopping()
        {
            StorePageFlow.Suspend();
        }

        public void CompleteCheckout(string name, string address, string creditCardNumber)
        {
            if (creditCardNumber != "111-111-111-111")
            {
                StorePageFlow.Navigate("failedCheckout");
                return;
            }
            StorePageFlow.Navigate("successCheckout");
        }

        public Product[] GetProductsCatalog()
        {
            List<Product> products = new List<Product>(10);
            products.Add(new Product(1, "1000", "Mobile01", (decimal)10.6, "Mobile01"));
            products.Add(new Product(2, "1001", "Mobile02", (decimal)10.6, "Mobile02"));
            products.Add(new Product(3, "1002", "Mobile03", (decimal)10.6, "Mobile03"));
            products.Add(new Product(4, "1003", "Mobile04", (decimal)10.6, "Mobile04"));
            products.Add(new Product(5, "1004", "Mobile05", (decimal)10.6, "Mobile05"));
            products.Add(new Product(6, "1005", "Mobile06", (decimal)10.6, "Mobile06"));
            products.Add(new Product(7, "1006", "Mobile07", (decimal)10.6, "Mobile07"));
            products.Add(new Product(8, "1007", "Mobile08", (decimal)10.6, "Mobile08"));
            products.Add(new Product(9, "1008", "Mobile09", (decimal)10.6, "Mobile09"));
            products.Add(new Product(10, "1009", "Mobile10", (decimal)10.6, "Mobile10"));
            return products.ToArray();
        }

        public void StartNewProcess()
        {
            StorePageFlow.Start();
            StorePageFlow.RedirectToCurrentPage();
        }

        public Product GetProductById(int productId)
        {
            List<Product> products = new List<Product>(GetProductsCatalog());
            return products.Find(new Predicate<Product>(delegate(Product product)
                                                            {
                                                                return product.ProductId == productId;
                                                            }));
        }

        public void BrowseCatalog()
        {
            StorePageFlow.Navigate("resumeShopping");
        }

        public ReadOnlyCollection<CartItemSummary> GetCartDetail()
        {
            List<CartItemSummary> summary = new List<CartItemSummary>();

            foreach (CartItem item in Cart)
            {
                summary.Add(
                    new CartItemSummary(item.Product.ProductId, item.Product.ModelName, item.Product.UnitPrice, item.Quantity,
                                        item.Product.UnitPrice * item.Quantity));
            }

            return summary.AsReadOnly();
        }

        public bool HasPageFlowInstanceSuspended()
        {
            return StorePageFlow.Status == PageFlowStatus.Suspended;
        }

        public void ResumeShopping()
        {
            StorePageFlow.Resume();
            StorePageFlow.RedirectToCurrentPage();
        }

        public void RestartShopping()
        {
            StorePageFlow.Abort(false);
            _storePageFlow = null;
            StartNewProcess();
        }

        public void FinishProcess()
        {
            StorePageFlow.Complete();
        }
    }
}